<?php

// Get variables
$client_id = get_input('client_id');
//$userid = get_input('user_id');
$userid = get_loggedin_userid();
$brief = get_input('brief');
$relationships = get_input('relationships');

$friendEntities = array();

if($client_id == OPINIONS) {
    $friend_requests = get_opinions_friends_of($userid);
} else {
    $friend_requests = get_whiteboard_friends_of($userid);
}
foreach($friend_requests as $friend_request) {
    $friendEntities[] = get_user($friend_request->friend_id);
}

if($relationships) {
    $relationships = json_decode($relationships, TRUE);

    foreach($relationships as $relationship) {
        if($relationship == 'to') {
            if($client_id == OPINIONS) {
                $tos = get_opinions_friend_requests_to($userid);
            } else {
                $tos = get_whiteboard_friend_requests_to($userid);
            }
            foreach ($tos as $to) {
                $friendEntities[] = get_user($to->friend_id);
            }
        }
        if($relationship == 'from') {
            if($client_id == OPINIONS) {
                $froms = get_opinions_friend_requests_from($userid);
            } else {
                $froms = get_whiteboard_friend_requests_from($userid);
            }
            foreach ($froms as $from) {
                $friendEntities[] = get_user($from->user_id);
            }
        }
    }
}

$friends = array();

foreach($friendEntities as $friendEntity) {
	$friends[] = userToArray($friendEntity, $client_id, $brief);
}

$count = count($friends);

echo json_encode(export_result(200, $friends));

?>